package com.oocl.tm.util;

import java.io.IOException;
import java.io.InputStream;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.tm.log4j.Log4j;
import com.oocl.tm.protocol.MsgPackage;

public class JsonUtil {
	
	private static ObjectMapper mapper = new ObjectMapper();
	
	
	public static String parserToString(MsgPackage mp){
		String jsonStr = null;
		try {
			jsonStr = mapper.writeValueAsString(mp);
		} catch (JsonProcessingException e) {
			Log4j.getLogger().error(e, e.fillInStackTrace());
		}
		return jsonStr;
	}
	
	public static MsgPackage streamToMsgPackage(InputStream input){
		MsgPackage mp = null;
		try {
			mp = mapper.readValue(input, MsgPackage.class);
		} catch (IOException e) {
			Log4j.getLogger().error("[ error while parser stream to msgPackage ]");
			Log4j.getLogger().error(e, e.fillInStackTrace());
		}
		return mp;
	}
	
}
